/**
 * 全局数据
 * by wyy
 */
class globalObject {
    private static instance: globalObject;

    player: { Deskid: number, Userid: number, Username: string, Rolltime: number, Result: string }
        = { Deskid: 1, Userid: 0, Username: "未登录", Rolltime: 0, Result: "" };
    debug: boolean = false;

    showRank: boolean = true;

    diceFactory: egret.MovieClipDataFactory;

    resultListArr: eui.ArrayCollection = new eui.ArrayCollection();
    //界面的监听
    createDeskListener: boolean = true;
    createHallListener: boolean = true;
    createIntroListener: boolean = true;

    static getInstance(): globalObject {
        (!this.instance) && (this.instance = new globalObject());
        return this.instance;
    }

    gameColor = {
        white: 0xffffff,
        lightGrey: 0x8A8988,
        lightBrown: 0x9c5f33,
        deepBrown: 0x864f27,
        red: 0xCE2F24
    }

    constructor() { }

    //显示文字提示
    showTip(s: string) {
        let stage = egret.MainContext.instance.stage;
        let w = window.innerWidth;
        let h = window.innerHeight;
        let grp = new eui.Group();
        let lbl = new eui.Label(s);
        lbl.textColor = 0xFFFFFF;
        lbl.size = 50;
        let bg = new eui.Rect(lbl.width, lbl.height, 0x9C5F33);
        bg.ellipseHeight = 20;
        bg.ellipseWidth = 20;
        grp.addChild(bg);
        grp.addChild(lbl);
        grp.x = w / 2
        grp.y = h / 2;
        stage.addChild(grp);
        grp.alpha = 0;
        grp.anchorOffsetX = grp.width / 2;
        grp.anchorOffsetY = grp.height / 2;
        egret.Tween.get(grp).to({
            alpha: 0.8
        }, 500).to({
            alpha: 0
        }, 1000).call(() => { stage.removeChild(grp) });
    }

    /**
     * 调试输出
     * @param s 提示字符串
     * @param d 对象
     */
    debugConsole(s, d = {}) {
        (this.debug) && (console.log(s, d));
    }
    /**
     * 时间戳换算时间 输出字符串
     */
    timestampToTimeString(timestamp) {
        if (timestamp == 0) {
            return "未投掷";
        }
        let date = new Date(timestamp * 1000);
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return h + m + s;
    }
    /**
     * 点数排序
     */
    resultSort(s) {
        let list: Array<string> = s.split(",").sort().reverse();
        if (list.length < 3) {
            list.push("0");
            list.push("0");
        }
        return list.toString();
    }
    /**
     * 根据工号算头像
     */
    idToHeadId(id): number {
        let hId = 0;
        /*if (!id) {
            return hId;
        }*/
        let idArr = id.toString().split("");
        for (let i of idArr) {
            hId += Number(i);
        }
        while (hId > 10) {
            hId %= 10;
        }
        return hId;
    }
}


